package genedit.controller;

import genedit.view.graph.GraphWorkArea;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

/**
 * Klasa enkapsulira ponašanje prilikom stanja zumiranja.
 * 
 * @author Nikola
 * @version 1.1 18/12/13
 */
public class ZoomState extends State {

	/**
	 * Smer zumiranja; 1 = Zoom in, -1 = Zoom out
	 */
	int zoomDirection = 1;

	/**
	 * Prilikom otpuštanja miša radna površina se skalira u zavisnosti od smera
	 * zumiranja. Zumiranje se takođe izvršava u pravcu pozicije miša.
	 */
	public void mouseReleased(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1) {
			Point2D oldPosition = e.getPoint();
			context.pointToUserSpace(oldPosition);

			double scale = context.getScaling();
			context.setScaling((zoomDirection > 0 ? scale / 0.8 : scale * 0.8));
			context.setupTransformation();

			Point2D newPosition = e.getPoint();
			context.pointToUserSpace(newPosition);

			context.setTranslateX(context.getTranslateX() + newPosition.getX() - oldPosition.getX());
			context.setTranslateY(context.getTranslateY() + newPosition.getY() - oldPosition.getY());

			context.setupTransformation();
			context.repaint();
		} else if (e.getButton() == MouseEvent.BUTTON3)
			context.getController().setCurrentState(new SelectionState(context));
	}

	public void keyPressed(KeyEvent e) {

	}

	public ZoomState(GraphWorkArea context, int direction) {
		super(context);
		this.zoomDirection = direction;
	}

}